// 使用 readline 来创建一个简单的、命令驱动得用户界面

let readline = require('readline')

let rl = readline.createInterface(process.stdin, process.stdout);

// ask question

rl.question(">>what is the meaning of life?", (answer) => {
  console.log("About the meaning of life, you said " + answer)
  rl.setPrompt(">>")
  rl.prompt()
})

// function to close interface 

function closeInterface() {
  rl.close()
  console.log('leaving readline')
}

// listen for .leave
rl.on('line', (cmd) => {
  if (cmd.trim() == '.leave') {
    closeInterface()
    return
  }
  console.log("repeating command:" + cmd)
  rl.prompt()
})

rl.on('close', () => {
  closeInterface()
})


